﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.IO;



namespace Text2String
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private ConfigFile config = default(ConfigFile);

        private string ProgressText(string inputStr)
        {
            string outputStr = string.Empty;

            foreach (char c in inputStr)
            {
                // 对每个字符进行处理
                switch (c)
                {
                    case '\t':
                        outputStr += "\\t";
                        break;

                    case '\n':
                        outputStr += "\\n";
                        break;

                    case '\\':
                        outputStr += "\\\\";
                        break;

                    case '\"':
                        outputStr += "\\\"";
                        break;

                    default:
                        outputStr += c;
                        break;
                }

                // 这里可以添加更多的对字符的操作
            }
            return outputStr;
        }

        private void MyTextChanged()
        {
            this.outoutBox.Text = "\"" + ProgressText(this.textBox_head.Text) + ProgressText(this.inputBox.Text) + ProgressText(this.textBox_Tail.Text) + "\"";
        }
        private void inputBox_TextChanged(object sender, EventArgs e)
        {
            MyTextChanged();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            inputBox.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Copy2Clipboard();
        }

        private void Copy2Clipboard()
        {
            // 将文本复制到剪切板
            if(this.outoutBox.Text != string.Empty)
            {
                Clipboard.SetText(this.outoutBox.Text);
            }
            
            if(this.checkBox_autoMinimized.CheckState == CheckState.Checked)
            {
                Minimized();
            }
        }

        private void Minimized()
        {
            this.WindowState = FormWindowState.Minimized;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape && this.ContainsFocus)
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if(!this.checkBox_CopyFromClipboard.Checked)
            {
                // 如果不监视剪切板就退出
                return;
            }

            inputBox.Clear();
            if (Clipboard.ContainsText())
            {
                inputBox.Text = Clipboard.GetText();
            }

        }

        private bool DontActWhileLoading = false;
        private void checkBox_CopyFromClipboard_CheckedChanged(object sender, EventArgs e)
        {
            if (DontActWhileLoading)
                return;
            config.WatchClipboard = this.checkBox_CopyFromClipboard.Checked;
            config.Write();
        }

        private void checkBox_autoMinimized_CheckedChanged(object sender, EventArgs e)
        {
            if (DontActWhileLoading)
                return;
            config.AutoMinimizedAfterCopy = this.checkBox_autoMinimized.Checked;
            config.Write();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DontActWhileLoading = true;
            config = new ConfigFile();
            this.checkBox_autoMinimized.Checked = config.AutoMinimizedAfterCopy;
            this.checkBox_CopyFromClipboard.Checked = config.WatchClipboard;
            DontActWhileLoading = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox_head.Clear();
            MyTextChanged();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.textBox_Tail.Clear();
            MyTextChanged();
        }

        private void textBox_head_TextChanged(object sender, EventArgs e)
        {
            MyTextChanged();
        }

        private void textBox_Tail_TextChanged(object sender, EventArgs e)
        {
            MyTextChanged();
        }
    }
}
